package com.aidong.web.controller.dic;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.PtCoachCode;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptCoach"})
public class PtCoachController extends BaseController {
    @Resource
    PtCoachCodeDao ptCoachCodeDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;

    public PtCoachController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("dic/coach/list")).addObject("coachLevel", this.getTypeList("coachLevel")).addObject("coachType", this.getTypeList("coachType"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "coachLevel",required = false) String coachLevel, @RequestParam(value = "coachType",required = false) String coachType, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.gt("status", Integer.valueOf(0));
        cd.eq("coachLevel", coachLevel);
        cd.eq("coachType", coachType);
        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("code", "%" + name + "%");
            mv.like("chName", "%" + name + "%");
            cd.addSub(mv);
        }

        this.ptCoachCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv1 = new ModelAndView("dic/coach/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        PtCoachCode coach = new PtCoachCode();
        coach.setStatus(Integer.valueOf(1));
        List shopList = this.dicShopCodeDao.getAllShopList();
        Iterator mv = shopList.iterator();

        while(mv.hasNext()) {
            DicShopCode shop = (DicShopCode)mv.next();
            shop.setStatus(Integer.valueOf(0));
        }

        ModelAndView mv1 = new ModelAndView("dic/coach/edit");
        mv1.addObject("coach", coach);
        mv1.addObject("shopList", shopList);
        mv1.addObject("coachType", this.getTypeList("coachType"));
        mv1.addObject("coachLevel", this.getTypeList("coachLevel"));
        mv1.addObject("_oper", "add");
        return mv1;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("coach") PtCoachCode coach, @RequestParam(value = "shops",required = false) String shops) {
        if(this.chkNull(coach.getCode())) {
            return this.getResult(false, "请输入教练编码");
        } else if(this.chkNull(coach.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNull(coach.getCoachLevel())) {
            return this.getResult(false, "请选择等级");
        } else if(this.chkNull(coach.getCoachType())) {
            return this.getResult(false, "请选择类型");
        } else {
            if(this.chkNull(coach.getStatus())) {
                coach.setStatus(Integer.valueOf(1));
            }

            if(this.ptCoachCodeDao.exists(coach.getCode())) {
                return this.getResult(false, "教练编码已存在");
            } else {
                coach.setCreateUser(this.getLoginUserCode());
                coach.setCreateTime(new Date());
                coach.setUpdateTime(new Date());
                ArrayList shopList = new ArrayList();
                if(!this.chkNull(shops)) {
                    String[] e = shops.split(",");
                    String[] arr$ = e;
                    int len$ = e.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        String s = arr$[i$];
                        if(!this.chkNull(s)) {
                            shopList.add(s.trim());
                        }
                    }
                }

                try {
                    this.ptCoachCodeDao.add(coach);
                    this.ptCoachCodeDao.updateCoachShop(coach.getCode(), shopList);
                    this.reloadTypeList("ptCoachName");
                    this.addUserLog("新增教练", "编号:" + coach.getCode() + ",名称:" + coach.getChName());
                } catch (Exception var9) {
                    this.log.error("add diccoach", var9);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("code") String code) {
        PtCoachCode coach = this.ptCoachCodeDao.getById(code);
        if(coach == null) {
            return this.showAlert("没有这个教练");
        } else {
            List shopList = this.ptCoachCodeDao.getCoachShopList(code, true);
            ModelAndView mv = new ModelAndView("dic/coach/edit");
            mv.addObject("coach", coach);
            mv.addObject("shopList", shopList);
            mv.addObject("coachType", this.getTypeList("coachType"));
            mv.addObject("areaType", this.getTypeList("areaType"));
            mv.addObject("coachLevel", this.getTypeList("coachLevel"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("coach") PtCoachCode coach, @RequestParam(value = "shops",required = false) String shops) {
        if(this.chkNull(coach.getCode())) {
            return this.getResult(false, "没有这个教练");
        } else if(this.chkNull(coach.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNull(coach.getCoachLevel())) {
            return this.getResult(false, "请选择等级");
        } else if(this.chkNull(coach.getCoachType())) {
            return this.getResult(false, "请选择类型");
        } else {
            if(this.chkNull(coach.getStatus())) {
                coach.setStatus(Integer.valueOf(1));
            }

            PtCoachCode coach1 = this.ptCoachCodeDao.getById(coach.getCode());
            if(coach1 == null) {
                return this.getResult(false, "没有这个教练");
            } else {
                coach1.setChName(coach.getChName());
                coach1.setCoachLevel(coach.getCoachLevel());
                coach1.setCoachType(coach.getCoachType());
                coach1.setNote(coach.getNote());
                coach1.setStatus(coach.getStatus());
                coach1.setUpdateTime(new Date());
                ArrayList shopList = new ArrayList();
                if(!this.chkNull(shops)) {
                    String[] e = shops.split(",");
                    String[] arr$ = e;
                    int len$ = e.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        String s = arr$[i$];
                        if(!this.chkNull(s)) {
                            shopList.add(s.trim());
                        }
                    }
                }

                try {
                    this.ptCoachCodeDao.update(coach1);
                    this.ptCoachCodeDao.updateCoachShop(coach1.getCode(), shopList);
                    this.reloadTypeList("ptCoachName");
                    this.addUserLog("修改教练", "编号:" + coach1.getCode() + ",名称:" + coach1.getChName());
                } catch (Exception var10) {
                    this.log.error("edit diccoach", var10);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        PtCoachCode coach = this.ptCoachCodeDao.getById(code);
        if(coach == null) {
            return this.getResult(false, "没有这个教练");
        } else {
            try {
                this.ptCoachCodeDao.updateStatus(code, 0);
                this.reloadTypeList("ptCoachName");
                this.addUserLog("删除教练", "编号:" + coach.getCode() + ",名称:" + coach.getChName());
            } catch (Exception var4) {
                this.log.error("del diccoach", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}